package his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import his.entity.Disease;
import his.entity.DiseCategory;
import his.persistence.DieaseMapper;
import his.service.DieaseService;
import org.example.his.common.api.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DieaseServiceImpl implements DieaseService {

    @Autowired
    DieaseMapper dieaseMapper;

    @Override
    public CommonResponse<List<Disease>> getall_diease_bycodename(String keywords) {
        QueryWrapper<Disease> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("DiseaseCode",keywords);
        List<Disease> diseaseList1 =dieaseMapper.selectList(queryWrapper1);
        if(diseaseList1.size()>0){
            return CommonResponse.createForSuccess(diseaseList1);
        }else{
            QueryWrapper<Disease> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("DiseaseName",keywords);
            List<Disease> diseaseList2=dieaseMapper.selectList(queryWrapper2);
            if(diseaseList2.size()>0){
                return CommonResponse.createForSuccess(diseaseList2);
            }else{
                return CommonResponse.createForError("搜索失败");
            }
        }
    }

    @Override
    public CommonResponse<List<Disease>> delete_diease(int id) {
        QueryWrapper<Disease> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("ID",id);
        List<Disease> diseaseList1=dieaseMapper.selectList(queryWrapper1);
        if(diseaseList1.size()>0){
            QueryWrapper<Disease> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("ID",id);
            int result=dieaseMapper.delete(queryWrapper2);
            return CommonResponse.createForSuccess(diseaseList1);
        }else{
            return CommonResponse.createForError("删除失败");
        }
    }

    @Override
    public CommonResponse<List<Disease>> update_diease( int id,  String diseasecode,  String diseasename,  String diseaseICD,  int disecategoryID) {
        QueryWrapper<Disease> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("ID",id);
        List<Disease> diseaseList=dieaseMapper.selectList(queryWrapper1);
        if(diseaseList.size()>0){
            QueryWrapper<Disease> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("ID",id);
            Disease disease=new Disease();
            disease.setDiseaseICD(diseaseICD);
            disease.setDiseaseCode(diseasecode);
            disease.setDiseaseName(diseasename);
            disease.setDiseCategoryID(disecategoryID);

            int result=dieaseMapper.update(disease,queryWrapper2);
            QueryWrapper<Disease> queryWrapper3=new QueryWrapper<>();
            queryWrapper3.eq("ID",id);
            List<Disease> diseaseList1=dieaseMapper.selectList(queryWrapper3);
            return CommonResponse.createForSuccess(diseaseList1);
        }else{
            return CommonResponse.createForError("修改失败");
        }
    }

    @Override
    public CommonResponse<List<Disease>> add_diease( String diseasecode,  String diseasename,  String diseaseICD,  int disecategoryID) {
        QueryWrapper<Disease> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("DiseaseName",diseasename);
        List<Disease> diseaseList1=dieaseMapper.selectList(queryWrapper1);
        if(diseaseList1.size()>0){
            return CommonResponse.createForError("新增失败，名称已存在");
        }else{
            QueryWrapper<Disease> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("DiseaseCode",diseasecode);
            List<Disease> diseaseList2=dieaseMapper.selectList(queryWrapper2);
            if(diseaseList2.size()>0){
                return CommonResponse.createForError("新增失败，编号已存在");
            }else{
                Disease disease=new Disease();
                disease.setDiseaseCode(diseasecode);
                disease.setDiseaseName(diseasename);
                disease.setDiseaseICD(diseaseICD);
                disease.setDiseCategoryID(disecategoryID);
                disease.setDelMark(1);
                dieaseMapper.insert(disease);
                QueryWrapper<Disease> queryWrapper3=new QueryWrapper<>();
                queryWrapper3.eq("DiseaseCode",diseasecode);
                List<Disease> diseaseList3=dieaseMapper.selectList(queryWrapper3);
                return CommonResponse.createForSuccess(diseaseList3);
            }
        }
    }

    @Override
    public CommonResponse<List<Disease>> getall_diease() {
        QueryWrapper<Disease> queryWrapper=new QueryWrapper<>();
        List<Disease> diseaseList=dieaseMapper.selectList(queryWrapper);
        if(diseaseList.size()>0){
            return CommonResponse.createForSuccess(diseaseList);
        }else{
            return CommonResponse.createForError("查询失败");
        }
    }
}
